//
//  UniPartyViewController.m
//  RegBars
//
//  Created by Huu Nhat on 9/25/11.
//  Copyright 2011 __MyCompanyName__. All rights reserved.
//

#import "UniPartyViewController.h"
#import "RegBarsAppDelegate_iPhone.h"
#import "PartyCell.h"
#import "Party.h"
#import "CitiesViewController.h"

@implementation UniPartyViewController
@synthesize partyCell;
@synthesize partyArray;
@synthesize partyTable;
@synthesize thumbnailArray;

- (void)didEndGetParty:(NSMutableArray *)Array{
	[partyArray setArray:Array];
	self.thumbnailArray = [[NSMutableArray alloc] init];
	for (int i = 0; i < [partyArray count]; i++) {
		Party *party = [partyArray objectAtIndex:i];
		NSLog(@"Thum %@", party.thumbnailUrl);
		NSData *imageData = [NSData dataWithContentsOfURL:[NSURL URLWithString:party.thumbnailUrl]];
		UIImage *image = [[UIImage alloc]initWithData:imageData];
		if (image != nil) {
			[thumbnailArray addObject:image];
			[image release];
		}
	}
	[partyTable reloadData];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
	return [partyArray count];
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
	return 70;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
	static NSString *partyCellID = @"partyCellID";
	partyCell = (PartyCell*)[partyTable dequeueReusableCellWithIdentifier:partyCellID];
	if (partyCell == nil) {
		[[NSBundle mainBundle] loadNibNamed:@"PartyCell" owner:self options:nil];
	}
	Party *party = [partyArray objectAtIndex:indexPath.row];
	[partyCell.partyIdLabel setText:[NSString stringWithFormat:@"Bild%@",[party.partyId stringByReplacingOccurrencesOfString:@" " withString:@""]]];
	[partyCell.timeLabel setText:[party.time stringByReplacingOccurrencesOfString:@"  " withString:@""]];
	[partyCell.nameLabel setText:[party.name stringByReplacingOccurrencesOfString:@"  " withString:@""]];
	[partyCell.partyThumbnail setImage:[thumbnailArray objectAtIndex:indexPath.row]];
	return partyCell;
}

- (IBAction)backToCity:(id)sender
{
//    [mainAppDelegate_iPhone.citiesViewController flipToHome];
}


// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
    [super viewDidLoad];
	partyArray = [[NSMutableArray alloc] init];
	UniPartyParser *parser = [[UniPartyParser alloc] init];
	[parser getPartysFromURL:UniPartys_URL withDelegate:self];
}

- (void)didReceiveMemoryWarning {
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
    
    // Release any cached data, images, etc. that aren't in use.
}

- (void)viewDidUnload {
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}


- (void)dealloc {
	[thumbnailArray release];
	self.partyCell = nil;
	self.partyTable = nil;
	[partyArray release];
    [super dealloc];
}


@end
